package com.li.controller;

import com.li.common.config.Result;
import com.li.mapper.UserMapper;
import com.li.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
  @Autowired
  UserMapper userMapper;
  
  //登陆验证
  @PostMapping("/login")
  public Result<?> login(@RequestBody User user) {
    if (user.getRole() == 0) { //学生登录
      
      User res = userMapper.loginSt(user.getUserId(), user.getPassword());
      if (res == null) {
        return Result.error("-1", "学生的用户名或密码错误");
      }
      return Result.success(res);
    } else {
      User res = userMapper.loginAd(user.getUserId(), user.getPassword());
      if (res == null) {
        return Result.error("-1", "管理员的用户名或密码错误");
      }
      return Result.success(res);
    }
  }
  
  @GetMapping("/student")
  public Result<?> getStudents(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer rows,
                               @RequestParam(defaultValue = "")  String name) {
    Map<String, Object> ret = new HashMap<String, Object>();
    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("name", "%" + name + "%");
    queryMap.put("offset", ((page -1)*rows));
    queryMap.put("pageSize", rows);
    ret.put("rows", userMapper.findStuList(queryMap));
    ret.put("total", userMapper.getStuTotal(queryMap));
    return Result.success(ret);
  }
  
  @GetMapping("/admin")
  public Result<?> getAdmins(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer rows,
                               @RequestParam(defaultValue = "")  String name) {
    Map<String, Object> ret = new HashMap<String, Object>();
    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("name", "%" + name + "%");
    queryMap.put("offset", ((page -1)*rows));
    queryMap.put("pageSize", rows);
    ret.put("rows", userMapper.findAdmList(queryMap));
    ret.put("total", userMapper.getAdmTotal(queryMap));
    return Result.success(ret);
  }
  
  @PutMapping
  public Result<?> updateUser(@RequestBody User user) {
    System.out.println(user);
    Integer i  = userMapper.updateUser(user);
    if (i == 0) {
      return Result.error("-1", "更新用户失败!");
    }
 
    return Result.success(i);
  }
  
  @PutMapping("/admin")
  public Result<?> updateAdmin(@RequestBody User user) {
    System.out.println(user);
    Integer i  = userMapper.updateUserAdmin(user);
    if (i == 0) {
      return Result.error("-1", "更新用户失败!");
    }
    
    return Result.success(i);
  }
  
  @PostMapping
  public Result<?> addUser(@RequestBody User user) {
    try {
      userMapper.addUser(user);
      //添加学号到宿舍
      userMapper.addDorm(user);
      //添加学号到打卡
      userMapper.addCard(user);
      return Result.success();
    }catch (DataAccessException e){
      return Result.error("-1","添加失败!已有当前学号");
    }
    
  }
  
  @DeleteMapping("/{id}")
  public Result<?> deleteUser(@PathVariable Integer id) {
    Integer i = userMapper.deleteById(id);
    if (i == 0){
      return Result.error("-1", "删除失败");
    }
    return Result.success();
  }
  
  @PostMapping("/deleteBatch")
  public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
    System.out.println(ids);
    Integer i =  userMapper.deleteBatch(ids);
    if (i == 0){
      return Result.error("-1", "批量删除失败");
    }
    
    return Result.success();
  }
  
}
